(function ($) {

    var imageDetails = function(response) {
      var data = response.data;
      if (data != ''){
      	  var id = response.nid;
      	  var timestamp = response.newTimeStamp;
      	  var fieldName = response.fieldName;
      	  $('#' + id).attr('name', fieldName + '^' + timestamp);
      	  Drupal.canvas_fields[id].setImage(response.data);
            $('#drawCanvas').val(response.data);
      }
    }
    
    var loadImage = function(nid, field_name) {
  	  //$.get('/canvas/load/'+ parseInt(nid, 10) + '/' + field_name, null, imageDetails);
  	 $.ajax({
           type: "GET",
           url: "/canvas/load/",
           dataType: "json",
           success: function(response){
           },

           data: 'nid=' + parseInt(nid, 10) + '&field_name=' + field_name,
       });
  	  return false;
    }
    
    $.autoSaveLoadCanvas = function() {
    	if($('#overlay-container').length <= 0){
    		$('input.drawing-board').each(function() {
    			var id = this.id;
    			var field_name = this.name;
    			loadImage(id, field_name);;
    		});
    	}
  	// Schedule this function to be run again in 1 sec
        //setTimeout($.autoSaveLoadCanvas, 1000);
    }
})(jQuery);